<template>
	<div class="system-menu-dialog-container">
		<el-dialog :title="state.dialog.title" @close="closeDialog" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="menuDialogFormRef" :model="ruleForm" size="default" label-width="120px" :rules="rules">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="区域编号：" prop="number">
							<el-input :disabled="disabledFlag" size="default"  placeholder="区域编号" style="max-width: 400px" v-model="ruleForm.number"> </el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="区域名称：" prop="name">
							<el-input size="default" placeholder="区域名称" style="max-width: 400px" v-model="ruleForm.name"> </el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="启用状态：" prop="status">
							<el-select clearable v-model="ruleForm.status"  placeholder="启用状态" style="width: 400px;">
								<el-option label="停用" :value="0"></el-option>
								<el-option label="启用" :value="1"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemRoleDialog">
import { onMounted, reactive, ref ,defineProps,watch,defineEmits} from 'vue';
import {  ElMessage, type FormRules } from 'element-plus'
import {useBasicApi} from "/@/api/system/menu/index"
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
const BasicApi = useBasicApi()
const props = defineProps(['tableData','disabled'])
// dx定义start
	interface RuleForm {
		number:string,
		name:string,
		status:any,
	}
	const ruleForm = ref<RuleForm>({
		number:"",
		name:"",
		status:"",
	})
	const disabledFlag = ref(false)

	const rules = reactive<FormRules<RuleForm>>({
		number:[
			{ required: true, message: '区域编号必填', trigger: 'blur' },
		],
		name:[
			{ required: true, message: '区域名称必填', trigger: 'blur' },
		],
		status:[
			{ required: true, message: '启用状态必填', trigger: 'blur' },
		],
	})

	// 监听父组件传递过来的数据变化
	watch(props,(newValue)=>{
		ruleForm.value = newValue.tableData
		//console.log(ruleForm.value)
		disabledFlag.value = newValue.disabled
	})
// dx定义end
const menuDialogFormRef = ref(null)
// 定义变量内容
const state = reactive({
	ruleForm: {
		roleName: '', // 角色名称
		roleSign: '', // 角色标识
		sort: 0, // 排序
		status: true, // 角色状态
		describe: '', // 角色描述
	},
	menuData: [] as TreeType[],
	menuProps: {
		children: 'children',
		label: 'label',
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = () => {
		state.dialog.title = '新增区域';
		state.dialog.submitTxt = '新 增';
		state.dialog.isShowDialog = true;
		// 清空表单，此项需加表单验证才能使用
		// nextTick(() => {
		// 	menuDialogFormRef.value.resetFields();
		// });
	}
	// getMenuData();;
// 关闭弹窗
const closeDialog = () => {
	menuDialogFormRef.value.resetFields()
	state.dialog.isShowDialog = false;
	// emit('refresh')
	
};
// 取消
const onCancel = () => {
	closeDialog();
};

// 提交
const onSubmit = (async ()=>{
	// 添加
		let res = await BasicApi.BasicAddApi(ruleForm.value);
		if(res.isSuccess){
			ElMessage.success('添加成功')
			closeDialog();
			emit('refresh');
		}else{
			ElMessage.error(res.returnMsg)
		}
	
})

onMounted(()=>{
	
})


// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style scoped lang="scss">
.system-role-dialog-container {
	.menu-data-tree {
		width: 100%;
		border: 1px solid var(--el-border-color);
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}
}
</style>
